﻿using ConfigCreate.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ConfigCreate
{
    /// <summary>
    /// 
    /// </summary>
    public struct DynamicBindingRoutePath
    {
        /// <summary>
        /// 绑定对象
        /// </summary>
        [JsonIgnore]
        public object? BindingObject;
        /// <summary>
        /// 属性路径
        /// </summary>
        [JsonProperty("PP")]
        public string PropertyPath = string.Empty;
        /// <summary>
        /// 绑定路径
        /// </summary>
        [JsonProperty("BP")]
        public string BindingRoutePath = string.Empty;
        /// <summary>
        /// 大类别
        /// </summary>
        [JsonProperty("HN")]
        public string HeaderName = string.Empty;
        /// <summary>
        /// 小类别
        /// </summary>
        [JsonProperty("GN")]
        public string GroupName = string.Empty;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="binding_object"></param>
        /// <param name="property_path"></param>
        /// <param name="binding_route_path"></param>
        /// <param name="header_name"></param>
        /// <param name="group_name"></param>
        public DynamicBindingRoutePath(object? binding_object, string property_path, string binding_route_path, string header_name, string group_name)
        {
            BindingObject = binding_object;
            PropertyPath = property_path;
            BindingRoutePath = binding_route_path;
            HeaderName = header_name;
            GroupName = group_name;
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="binding_object"></param>
        /// <param name="value"></param>
        public void SetValue(object binding_object, object value)
        {
            if(binding_object != null)
            {
                var pi = binding_object.GetType().GetProperty(PropertyPath);
                if(pi != null)
                    pi.SetValue(binding_object, value);
                
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="panel"></param>
        /// <returns></returns>
        public bool CheckPanel(CreateControlBasePanel panel)
        {
            return panel.GroupHeader.HeaderName == HeaderName && panel.GroupHeader.GroupName == GroupName && panel.PropertyPath == PropertyPath;
        }
    }
}
